/*
 * Copyright (C) 2012 haru Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.harusource.di.delegate.impl;

import java.lang.reflect.Method;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.harusource.di.delegate.Delegate;

/**
 * This class implements AbsListView.OnScrollListener.</br>
 * It handles following events of AbsListView.</br>
 * </br>
 * <code>
 * void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)</br>
 * void onScrollStateChanged(AbsListView view, int scrollState)
 * </code>
 * </br>
 * </br>
 * @see android.widget.AbsListView.OnScrollListener#onScroll(AbsListView, int, int, int)
 * @see android.widget.AbsListView.OnScrollListener#onScrollStateChanged(AbsListView, int)
 * 
 * @author Hiroki Oizumi
 */
public class AbsListViewOnScrollListener extends Delegate implements OnScrollListener {
    private static final int EVENTHANDLE_ONSCROLLSATECHANGED = 1;

    private static final int EVENTHANDLE_ONSCROLL = 2;

    @Override
    public void add(Method template, Object target, Method eventhandle, View view) {
        super.add(template, target, eventhandle, view);
        ((AbsListView) view).setOnScrollListener(this);
    }

    @EventHandler(EVENTHANDLE_ONSCROLLSATECHANGED)
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        Delegate.invoke2(mTarget, mEventHandles, EVENTHANDLE_ONSCROLLSATECHANGED, view, scrollState);
    }

    @EventHandler(EVENTHANDLE_ONSCROLL)
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        Delegate.invoke4(mTarget, mEventHandles, EVENTHANDLE_ONSCROLL, view, firstVisibleItem, visibleItemCount, totalItemCount);
    }
}
